<html>

<style>
    * {
        margin: 0;
    }

    #content {
        color: white;
        position: absolute;
        left: -20px;
        top: -10px;
        white-space: pre-wrap;
        height: 200px;
        overflow: auto;
    }
</style>

<script type="importmap">
	{
	  "imports": {
		"three": "../../../three.js-r151/build/three.module.js",
		"three/addons/": "../../../three.js-r151/examples/jsm/"
	  }
	}
</script>

<body>
    <div id="content">
        .gltf格式文件几乎可以包含所有的三维模型相关信息的数据，比如模型层级关系、PBR材质、纹理贴图、骨骼动画、变形动画...
    </div>

    <div id="canvas"></div>

    <script type="module">
        import * as THREE from 'three';
        import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
        import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';

        // 1. 创建三大件
        // 1.1 三维场景Scene, 类似创建了个世界，三维世界
        const scene = new THREE.Scene();

        // 1.2 相机
        const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
        camera.position.set(3, 3, 3)

        // 1.3 渲染器
        const renderer = new THREE.WebGLRenderer();
        renderer.setSize(window.innerWidth, window.innerHeight);
        let canvasDom = document.getElementById("canvas");
        canvasDom.appendChild(renderer.domElement);

        // 设置相机控件轨道控制器OrbitControls
        new OrbitControls(camera, canvasDom);

        // 添加辅助坐标系
        const axesHelper = new THREE.AxesHelper(5);
        scene.add(axesHelper);

        function render() {
            renderer.render(scene, camera);
            requestAnimationFrame(render)
        }
        render();

        // 2. 打光
        {
            const light = new THREE.DirectionalLight(0xFFFFFF);
            light.position.set(6, 6, 6);
            scene.add(light)

            const hemiLight = new THREE.HemisphereLight(0xffffff, 0xffffff);
            hemiLight.position.set(0, 7, 0);
            scene.add(hemiLight);
        }


        // 3. 添加glb文件
        const loader = new GLTFLoader();
        loader.load('../../../three.js-r151/examples/models/gltf/Soldier.glb', function (gltf) {
            console.log("gltf", gltf);
            scene.add(gltf.scene);
            gltf.scene.rotateY(Math.PI * 5 / 4)

            // 骨骼辅助显示
            const skeletonHelper = new THREE.SkeletonHelper(gltf.scene);
            scene.add(skeletonHelper); 

            // 根据节点名字获取某个骨骼网格模型
const skinnedMesh = gltf.scene.getObjectByName("vanguard_Mesh");
console.log('骨架', skinnedMesh.skeleton);

            
        });


    </script>
</body>

</html>